<template>
  <q-layout>
    <q-layout-header ref="header">
      <xheader :title="title"></xheader>
    </q-layout-header>
    <q-page-container>
      <xscrollarea :scroll-height="scrollHeight">
        <q-page padding>
          <!--<q-select-->
            <!--v-model="adorgId"-->
            <!--float-label="报修车间"-->
            <!--filter-->
            <!--:options="adorgIdOptions" clearable-->
          <!--/>-->
          <!--<q-select-->
            <!--v-model="gongxuId"-->
            <!--float-label="工序"-->
            <!--:options="gongxuIdOptions" clearable-->
          <!--/>-->
          <q-select
            v-model="shebeiCode"
            float-label="设备编码-名称"
            :options="shebeiCodeOptions" clearable
          />
          <q-select v-show="faultTypeOptions.length"
                    v-model="faultType"
                    @input=""
                    float-label="故障类型"
                    :options="faultTypeOptions" clearable
          />
          <!--<q-select v-show="hualanIdOptions.length"-->
          <!--v-model="hualanId"-->
          <!--@input=""-->
          <!--float-label="花篮编码-名称"-->
          <!--:options="hualanIdOptions" clearable-->
          <!--/>-->

          <div v-show="name!==''">
            <br>
            <q-card>
              <q-card-title>
                设备名称：{{name}}
                <div slot="subtitle">
                  车间：{{bzName}}<br>
                  设备责任人：{{yhName}}
                </div>
              </q-card-title>
              <q-card-main>
                <q-item>
                  <q-item-main>
                    设备状态：
                    <q-radio v-model="type" val="N" label="运行"/>
                    <q-radio v-model="type" val="Y" label="停机"/>
                    <q-input float-label="故障说明" placeholder="选填" v-model="remark" type="textarea" rows="1"/>
                  </q-item-main>
                </q-item>

              </q-card-main>

            </q-card>
            <br>
            <q-btn :loading="submitLoading" label="提交" color="primary" class="full-width" @click="submit"/>
          </div>
        </q-page>
      </xscrollarea>
    </q-page-container>
  </q-layout>
</template>

<script>

  import Xheader from '../../components/xheader'
  import Xscrollarea from '../../components/xscrollarea'

  export default {
    name: 'failure_reporting',
    components: { Xscrollarea, Xheader },
    props: {
      title: {}
    },
    data: () => ({
      scrollHeight: 0,
      submitLoading: false,
      adorgId: '',
      adorgIdOptions: [],
      gongxuId: '',
      gongxuIdOptions: [],
      shebeiCode: '',
      shebeiCodeOptions: [],
      faultType: '',
      faultTypeOptions: [],
      hualanId: '',
      hualanIdOptions: [],
      name: '',
      bzName: '',
      yhName: '',
      type: 'N',
      remark: ''
    }),
    watch: {
      adorgId () {
        this.getGongxuIdOptions()
      },
      gongxuId () {
        this.getShebeiCodeOptions()
      },
      shebeiCode () {
        this.getInfo()
      }
    },
    methods: {
      getAdorgIdOptions () {
        this.axios.post(this.$conf.server + '/Troubleshooting/Equipmentcode', {}).then(res => {
          if (res.data.result) {
            for (let v of res.data.Plans) {
              this.shebeiCodeOptions.push({ value: v.CODE, label: v.NAME })
            }
            for (let v of res.data.faults) {
              this.faultTypeOptions.push({ value: v.TYPE, label: v.TYPE })
            }
            // for (let v of res.data.baskets) {
            //   this.hualanIdOptions.push({ value: v.CODE, label: v.CODE + '-' + v.NAME })
            // }
          }
        })
      },
      getGongxuIdOptions () {
        this.gongxuIdOptions = []
        this.shebeiCodeOptions = []
        this.name = ''
        this.bzName = ''
        this.yhName = ''
        this.type = 'N'
        this.remark = ''
        this.axios.post(this.$conf.server + '/Troubleshooting/EquipmentProcess',
          this.$qs.stringify({
            ORG_ID: this.adorgId
          })).then(res => {
          if (res.data.result) {
            for (let v of res.data.processname) {
              this.gongxuIdOptions.push({ value: v.GX_ID, label: v.NAME })
            }
          }
        })
      },
      getShebeiCodeOptions () {
        this.shebeiCodeOptions = []
        this.name = ''
        this.bzName = ''
        this.yhName = ''
        this.type = 'N'
        this.remark = ''
        this.axios.post(this.$conf.server + '/Troubleshooting/Equipmentcode',
          this.$qs.stringify({
            GXCODE: this.gongxuId
          })).then(res => {
          if (res.data.result) {
            for (let v of res.data.Plans) {
              this.shebeiCodeOptions.push({ value: v.CODE, label: v.CODE + '-' + v.NAME })
            }
          }
        })
      },
      getInfo () {
        this.name = ''
        this.bzName = ''
        this.yhName = ''
        this.type = 'N'
        this.remark = ''
        if (this.shebeiCode !== '') {
          this.axios.post(this.$conf.server + '/Troubleshooting/EquipmentInformation',
            this.$qs.stringify({
              CODE: this.shebeiCode
            })).then(res => {
            if (res.data.result) {
              this.name = res.data.failices[0].NAME
              this.bzName = res.data.failices[0].BZNAME
              this.yhName = res.data.failices[0].YHNAME
            }
          })
        }
      },
      submit () {
        this.submitLoading = true
        this.axios.post(this.$conf.server + '/Troubleshooting/SavaPendingMaintenanceList',
          this.$qs.stringify({
            SBCode: this.shebeiCode,
            HLCODE: this.hualanId,
            Remarks: this.remark,
            Equipmenttype: 'REPAIR',
            usernames: this.yhName,
            types: this.faultType,
            GongXuCODE: this.gongxuId,
            ZuZhiJiGou: this.adorgId,
            resultstype: this.type
          })).then(res => {
          this.submitLoading = false
          if (res.data.projectscounts === 'false') {
            this.$parent.warn('该设备未配置维修项目！')
          } else {
            if (res.data.counts === 'false') {
              this.$parent.warn('该设备已经报修！')
            } else {
              if (res.data.results === 'false') {
                this.$parent.error('报修失败')
              } else {
                this.$parent.info('报修成功')
                this.adorgId = ''
                this.gongxuId = ''
                this.gongxuIdOptions = []
                this.shebeiCode = ''
                this.shebeiCodeOptions = []
                this.faultType = ''
                this.hualanId = ''
                this.name = ''
                this.bzName = ''
                this.yhName = ''
                this.type = 'N'
                this.remark = ''
              }
            }
          }
        })
      }
    },
    mounted () {
      this.scrollHeight = this.$parent.webHeight - this.$refs.header.$el.clientHeight
    },
    created () {
      this.getAdorgIdOptions()
    }
  }
</script>

<style scoped>

</style>
